package com.lx.commonlib.mvp.view.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lx.commonlib.R;
import com.lx.commonlib.mvp.persenter.BasePresenter;
import com.lx.commonlib.utils.LogUtil;


/**
*  类说明：自带标题栏的Activity
*  created by liuxiong on 2017/11/14
**/
public abstract class ToolbarActivity<P extends BasePresenter> extends BaseActivity <P>{

    private LinearLayout mToobarRoot; //toolbar的根布局

    /**
     * MD标题栏
     */
    protected RelativeLayout mToolbar;
    /**
     * 设置标题
     */
    private TextView mTvTittle;

    public TextView mRightText;//右边的文字
    /**
     * all fragment root
     */

    public FrameLayout mRootFragmentLayout;
    /**
     * layout.xml inflater
     */
    protected LayoutInflater mLayoutInflater;
    /**
     * toolbar下面的分割线，用于隐藏toolbar的时候也隐藏分割线
     * created by liuxiong on 2016/11/4
     **/
    private View mSplitLine;
    private ImageView mTvBack;  //返回按钮


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.activity_toolbar);

        mToobarRoot = (LinearLayout) findViewById(R.id.llt_toolbar_root);
        mToolbar = (RelativeLayout) findViewById(R.id.toolbar);
        mTvTittle = (TextView)findViewById(R.id.tv_root_title);
        mRootFragmentLayout = (FrameLayout) findViewById(R.id.flt_toolbar_content_container);
        mRightText = (TextView) findViewById(R.id.tv_right_text);
        mSplitLine = findViewById(R.id.toolbar_split);
        mTvBack = (ImageView) findViewById(R.id.iv_back);

        //添加子类内容view
        mLayoutInflater = getLayoutInflater();
        int contentLayout = getContentLayoutId();
        if(contentLayout!=0){
            mLayoutInflater.inflate(contentLayout, mRootFragmentLayout, true);
        }

        mToolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onToolbarClick();
            }
        });

        if (isCanGoBack()) {
            setBackInconClickLisener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }else{
            mTvBack.setVisibility(View.INVISIBLE);
        }

        mToobarRoot.setFitsSystemWindows(isNeedFitWindow());
    }

    /**
     * 方法说明: 设置内容布局文件
     *
     * @param layoutResID 布局文件的资源Id
     * @see #setContentView(View)
     * @see #setContentView(View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentLayout(@LayoutRes int layoutResID) {
        mRootFragmentLayout.removeAllViews();
        View view = mLayoutInflater.inflate(layoutResID, mRootFragmentLayout, false);
        mRootFragmentLayout.addView(view);
    }

    public abstract  @LayoutRes int  getContentLayoutId();

    /**
    * 方法说明:  是否需要padding状态栏的高度
    * created by liuxiong on 2017/8/23 11:21
    */
    public boolean isNeedFitWindow(){
        return true;
    }

    /**
     * 设置 是否可以返回
     * 默认是 无法返回的
     *
     * @return
     */
    public boolean isCanGoBack() {
        return true;
    }


    public void setOnRightTextClickListener(View.OnClickListener onRightTextClickListener) {
        mRightText.setOnClickListener(onRightTextClickListener);
    }

    /**
     * toolbar点击事件
     * 子类可以复写
     */
    public void onToolbarClick() {
        LogUtil.e("base test");
    }

    /**
     * 设置返回按钮的监听
     *
     * @param onClickListener
     */
    public void setBackInconClickLisener(View.OnClickListener onClickListener) {
        mTvBack.setOnClickListener(onClickListener);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();//返回键
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
    * 方法说明:  设置页面标题
    * created by liuxiong on 2017/11/14 17:34
    */
    public void setTitle(String title){
        mTvTittle.setText(title);
    }
    /**
    * 方法说明:  设置页面标题
    * created by liuxiong on 2017/11/14 17:34
    */
    public void setTitle(@StringRes int title){
        mTvTittle.setText(title);
    }

    /**
     * 方法说明：隐藏ToolBar
     *
     * @author yaohuix
     * @time 2016/9/30 12:32
     */
    public void hideToolBar() {
        mToolbar.setVisibility(View.GONE);
        mSplitLine.setVisibility(View.GONE);
    }

    /**
     * 设置右边text的文字
     *
     * @param title
     */
    public void setRightText(String title) {
        mRightText.setText(title);
    }

    /**
    * 方法说明:  设置右边文字的颜色
    * created by liuxiong on 2017/9/3 15:00
    */
    public void setRightTextColor(int color){
        mRightText.setTextColor(color);
    }

    /**
     * 设置右边的view
     * @param view
     * @param layoutParams
     */
    public void setRightView(View view, RelativeLayout.LayoutParams layoutParams){
        mToolbar.addView(view,layoutParams);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightViewClick();
            }
        });
    }

    /**
     * 设置右边view的点击事件 需重写
     */
    public void rightViewClick(){

    }

}


